<template>
	<view class="container">
		<!-- 新建球局表单 start -->
		<view class="golf-title">基本设置</view>
		<baseSetting @gamesDate="gamesDate" @gamesTime="gamesTime" />
		<view class="golf-title">球手设置</view>
		<playerSetting :golfTournamentId="golfTournamentId" />
		<view class="golf-createGames">
			<button type="primary" @click="bindNext">下一步</button>
		</view>
		<!-- 新建球局表单 end -->
	</view>
</template>

<script>
	import baseSetting from '../../components/basicSetting/basicSetting.vue'
	import playerSetting from '../../components/playerSetting/playerSetting.vue'

	export default {
		components: {
			baseSetting,
			playerSetting
		},
		props: {},
		data() {
			return {
				isActive: false, // 是否删除组织者
				golfTournamentGalleryList: [], // 球员信息
				craeteDateTime: null, //开赛日期时间
				golfTournamentId: null, //高尔夫比赛ID
			}
		},
		onShareAppMessage(res) {
		    if (res.from === 'button') {// 来自页面内分享按钮
				console.log('分享')
				return {
				  title: '快来加入比赛吧！',
				  path: '/pages/score/score?golfTournamentId=' + this.golfTournamentId + '&type=2'
				}
		    }
		},
		created() {
			uni.$on('addPlayer',(data) => {
				this.golfTournamentGalleryList.push({
					golfGalleryId: data.golfGalleryId, //高尔夫观众ID
					golfTournamentId: this.golfTournamentId, //高尔夫比赛ID
					isOrganizer: 0, //是否组织者, 0-否, 1-是 true
					kickoffOrder: 0, //开球顺序,0-无顺序,仅球员大于0 true
					tee: 3, //tee位, 0-无位置, 1-黑,2-金,3-蓝,4-白,5-红
					tournamentIdentity: 1 //参与者身份, 0-观众, 1-球员
				})
				console.log('监听到事件来自 addPlayer ，携带参数 msg 为：' + data.golfGalleryId);
			})
			uni.$on('deletePlayer', data => {
				this.golfTournamentGalleryList.splice(Number(data) + 1, 1)
			})
			uni.$on('changeIsActive', data => {
				this.isActive = data
			})
			
			this.getCreateDate() //创建时间
			this.createGames() //创建球局
		},
		mounted() {},
		computed: {},
		beforeDestroy () {
			uni.$off('addPlayer')
			uni.$off('deletePlayer')
		},
		methods: {
			//创建新球场时间
			getCreateDate() {
				let nowDate = new Date();
				this.date = this.$utils.nowDate(nowDate)
				this.time = this.$utils.nowTime(nowDate, 1)
				this.craeteDateTime = `${this.date} ${this.time}`
			},

			//获取日期
			gamesDate(dateTime) {
				console.log('获取日期', dateTime)
				this.dateTime = dateTime;
			},

			//获取时间
			gamesTime(dateTime) {
				console.log('获取时间', dateTime)
				this.dateTime = dateTime;
			},
			//创建新球局
			async createGames() {
				let courseDefultInfo = uni.getStorageSync('courseDefultInfo')
				let golfGalleryId = uni.getStorageSync('golfGalleryId')
				let createGamesData = {
					'golfCourseId': Number(courseDefultInfo.golfCourseId), //球场ID
					'organizerId': Number(golfGalleryId), //组织者ID
					'teamNumber': 4, //同队人数,不得高于4人
					'teeTime': this.craeteDateTime, //预定的tee时间
					'tournamentRule': 0, //比赛规则,0-一般规则,1-大流氓,2-小流氓
					'tournamentStatus': 0, //比赛状态,0-待开球,1-待开始,2-进行中,3-已结束
					'tee': 3 //tee位, 0-无位置, 1-黑,2-金,3-蓝,4-白,5-红
				}
				// console.log('createGamesData', createGamesData)
				//创建新球局请求
				const {
					data: res
				} = await this.$api.games.gamesRequest({
					data: createGamesData
				})
				if (res.code === 0) {
					console.log('创建新球局', res)
					this.golfTournamentId = res.data.golfTournamentId
					let golfGalleryId = uni.getStorageSync('golfGalleryId')
					uni.setStorage({
						key:"golfTournamentGalleryId",
						data:res.data.currentTournamentGallery.golfTournamentGalleryId
					})
					this.golfTournamentGalleryList.push({
						golfGalleryId: golfGalleryId, //高尔夫观众ID
						golfTournamentId: this.golfTournamentId, //高尔夫比赛ID
						isOrganizer: 1, //是否组织者, 0-否, 1-是 true
						kickoffOrder: 0, //开球顺序,0-无顺序,仅球员大于0 true
						tee: 3, //tee位, 0-无位置, 1-黑,2-金,3-蓝,4-白,5-红
						tournamentIdentity: 1 //参与者身份, 0-观众, 1-球员
					})
					
				}
			},

			//下一步按钮提交球局信息
			async bindNext() {
				console.log('this.craeteDateTime', this.craeteDateTime)
				console.log('this.dateTime', this.dateTime);
				console.log('高尔夫比赛ID', this.isActive)
				let arr = JSON.parse(JSON.stringify(this.golfTournamentGalleryList))
				if (arr.length > 4 || this.isActive) {
					arr[0].tournamentIdentity = 0
				}
				for (let index in arr) {
					if (arr.length > 4 || this.isActive) {
						arr[index].kickoffOrder = Number(index)
					} else {
						arr[index].kickoffOrder = Number(index) + 1
					}
				}
				//创建高尔夫比赛下一步信息请求
				let courseDefultInfo = uni.getStorageSync('courseDefultInfo')
				let golfGalleryId = uni.getStorageSync('golfGalleryId')
				let nextGamesData = {
					golfCourseId: Number(courseDefultInfo.golfCourseId), //球场ID
					golfTournamentGalleryList: arr,
					golfTournamentId: this.golfTournamentId, //高尔夫比赛ID
					organizerId: Number(golfGalleryId), //组织者ID
					teamNumber: 4, //同队人数,不得高于4人
					teeTime: this.craeteDateTime, //预定的tee时间
					tournamentRule: 0, //比赛规则,0-一般规则,1-大流氓,2-小流氓
					tournamentStatus: 0, //比赛状态,0-待开球,1-待开始,2-进行中,3-已结束
				}
				const {
					data: res
				} = await this.$api.games.createNextRequest({
					data: nextGamesData,
				})
				if (res.code === 0) {
					console.log(res)
					uni.navigateTo({
						url: "/pages/arrange/arrange?golfTournamentId=" + res.data.golfTournamentId
					})
				}
			}
		}
	}
</script>

<style scoped>
	.golf-createGames {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
	}

	.golf-createGames button {
		border-radius: 0;
	}
</style>
